/**
*  \file mcu.h
*
*  \brief Controller abstraction Layer
*
*  \author Michael J. Hohmann <michael.hohmann@linde-mh.de>
*
*   This Unit provide an interface to the application to use hardware features.
*
*/
#ifndef MCU_H_INCLUDED
#define MCU_H_INCLUDED

/*================================================[ public includes  ]================================================*/
#include "../cfg/prj.h"
#include "../i2cgi/i2cgi.h"
/*================================================[ public defines   ]================================================*/


/*================================================[ public datatypes ]================================================*/
#include "xmc_gpio.h"

#define MCU_GPIO_PORT( port, pin )     (PORT0_BASE|(port<<8)|pin)

/** Digital inputs */
enum mcu_input
{
   EN_FORWARD_IN_D_CPU_V           = 0,  /**< FORWARD_IN_D_CPU_V         */
   EN_BOOT_ANCHOR_CPU_M            = 1,  /**< /BOOT_ANCHOR_CPU_M         */
   EN_SEAT_SWITCH_NC_IN_D_CPU_V    = 2,  /**< SEAT_SWITCH_NC_IN_D_CPU_V  */
   EN_NEUTRAL_POSITION_IN_D_CPU_V  = 3,  /**< NEUTRAL_POSITION_IN_D_CPU_V*/
   EN_REVERSE_IN_D_CPU_V           = 4,  /**< REVERSE_IN_D_CPU_V         */
   EN_LIFT_IN_D_CPU_V              = 5,  /**< LIFT_IN_D_CPU_V            */
   EN_TILT_IN_D_CPU_V              = 6,  /**< TILT_IN_D_CPU_V            */
   EN_MAST_AUX_1_IN_D_CPU_V        = 7,  /**< MAST_AUX_1_IN_D_CPU_V      */
   EN_MAST_AUX_2_IN_D_CPU_V        = 8,  /**< MAST_AUX_2_IN_D_CPU_V      */
   EN_SEAT_SWITCH_IN_D_CPU_V       = 9,  /**< SEAT_SWITCH_IN_D_CPU_V     */
   EN_RESERVE_1_IN_D_CPU_V         = 10, /**< RESERVE_1_IN_D_CPU_V       */
   EN_MAIN_CONTACTOR_ERROR         = 11, /**< MAIN_CONTACTOR_ERROR       */
   EN_CUT_OFF_ISO_VALVE_ERROR      = 12, /**< CUT-OFF_ISO_VALVE_ERROR    */
   EN_BUZZER_ERROR                 = 13, /**< BUZZER_ERROR               */
   EN_CPU_SYNC                     = 14, /**< CPU_SYNC                   */
   EN_TLF_INT                      = 15, /**< TLF_INT                    */
   EN_PARK_BRAKE_IN_D_CPU_V        = 16, /**< PARK_BRAKE_IN_D_CPU_V      */
   EN_TLF_SAFESTATE1               = 17, /**< TLF_SATESTATE1             */
   EN_FAN_ERROR                    = 18, /**< FAN_ERROR                  */
};

/** Digital Outputs */
enum mcu_output
{
   mcu_output_FAN_CONTROLLER_OUT_LS_CPU_V       = MCU_GPIO_PORT( 0, 2  ), /**< FAN_CONTROLLER_OUT_LS_CPU_V */
   mcu_output_INV1_ENABLE_CPU_V                 = MCU_GPIO_PORT( 0, 3  ), /**< INV1_ENABLE_CPU_V           */
   mcu_output_DCDC_CONTROL_CPU_V                = MCU_GPIO_PORT( 0, 4  ), /**< DCDC_CONTROL_CPU_V          */
   mcu_output_BUZZER_OUT_LS_CPU_V               = MCU_GPIO_PORT( 0, 9  ), /**< BUZZER_OUT_LS_CPU_V         */
   mcu_output_FAN_MOTOR_OUT_LS_CPU_V            = MCU_GPIO_PORT( 0, 10 ), /**< FAN_MOTOR_OUT_LS_CPU_V      */
   mcu_output_I2C_WP_CPU_M                      = MCU_GPIO_PORT( 0, 12 ), /**< I2C_WP_CPU_M                */
   mcu_output_DC_LINK_PRECHAGE_EN_CPU_V         = MCU_GPIO_PORT( 1, 6  ), /**< DC_LINK_PRECHAGE_EN_CPU_V   */
   mcu_output_RESERVE_2_IN_ON_CPU_V             = MCU_GPIO_PORT( 1, 7  ), /**< RESERVE_2_IN_ON_CPU_V       */
   mcu_output_INV2_ENABLE_CPU_V                 = MCU_GPIO_PORT( 1, 9 ),  /**< INV2_ENABLE_CPU_V           */
   mcu_output_TLF_MPS_CON                       = MCU_GPIO_PORT( 2, 14 ), /**< TLF_MPS_CON                 */
   mcu_output_ALARM_BUZZER_OUT_LS_CPU_V         = MCU_GPIO_PORT( 2, 15 ), /**< ALARM_BUZZER_OUT_LS_CPU_V   */
   mcu_output_TLF_ERR                           = MCU_GPIO_PORT( 3, 5  ), /**< TLF_ERR                     */
   mcu_output_LED_0805_GE                       = MCU_GPIO_PORT( 3, 6  ), /**< LED_0805_GE                 */
   mcu_output_RESET_ERROR_CPU_V                 = MCU_GPIO_PORT( 5, 7  ), /**< RESET_ERROR_CPU_V           */
};


/** ADC Channels */
enum mcu_adc
{   /* do not change order. this order is the order of the input ports */
   mcu_adc_TempMotInv2       ,   /**< 0  TEMP_MOT_INV2_IN_A_CPU_V                  */
   mcu_adc_5VExtSns          ,   /**< 1  5V_EXTERNAL_SENSORS_A_CPU_V               */
   mcu_adc_DcLink            ,   /**< 2  DC_LINK_+_IN_A_CPU_V                      */
   mcu_adc_Accelerator1      ,   /**< 3  ACCELERATOR_1_IN_A_CPU_V                  */
   mcu_adc_13_7VSupply       ,   /**< 4  +13.7V_SUPPLY_A_CPU_V                     */
   mcu_adc_SteerAngleB       ,   /**< 5  STEER_ANGLE_B_IN_A_CPU_V                  */
   mcu_adc_lift_Pot          ,   /**< 6  RESERVE_2_IN_A_CPU_V                      */
   mcu_adc_TempMotInv1       ,   /**< 7  TEMP_MOT_INV1_IN_A_CPU_V                  */
   mcu_adc_CurrentCutOffIso  ,   /**< 8  CURRENT_CUT-OFF_ISO_VALVE_A_CPU_V         */
   mcu_adc_Accelerator2      ,   /**< 9  ACCELERATOR_2_IN_A_CPU_V                  */
   mcu_adc_CurrentMainCont   ,   /**< 10 CURRENT_MAIN_CONTACTOR_A_CPU_V            */
   mcu_adc_Reserve1          ,   /**< 11 RESERVE_1_IN_A_CPU_V                      */
   mcu_adc_SteerAngleA       ,   /**< 12 STEER_ANGLE_A_IN_A_CPU_V                  */
   mcu_adc_KeySwitch         ,   /**< 13 KEY_SWITCH_IN_A_CPU_V                     */
   mcu_adc_12VSupply         ,   /**< 14 12V_DRIVER_SUPPLY                  */
   mcu_adc_max_chn
};

/** PWM outputs */
enum mcu_pwm
{
   mcu_pwm_contactor    ,
   mcu_pwm_iso_valve    ,
   mcu_pwm_max
};

/*================================================[ public variables ]================================================*/

/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/
/** Initialization.
*
*  Initialize the mcu module.
*
*  \return RC_SUCCESS if successful.
*/
RC mcu_initialize( void );

/** Read Adc Channel
* \return current value of adc channel - No filter no average
*/
F32 mcu_get_adc( enum mcu_adc  channel );

/** Read input
*
* \param input pin
* \return TRUE pin is High - FALSE pin is low
*/
BO mcu_get_input( enum mcu_input pin );

/** Set Output
*
*/
void mcu_set_output( enum mcu_output pin , BO value );

/** Set PWM output
*
*/
void mcu_set_pwm(enum mcu_pwm channel, U16 value );

/** Get Reset Reason
*
* \return Reset Reason
*/
U32 mcu_get_reset_reason(void);

/** Get High resoltuion tick counter
*
* \return Tick Counter
*/
U32 mcu_high_resolution_counter(void);

/** get motor speed
*
* \return motor speed, units rpm
*/
S32 mcu_get_motor_speed(void);

U32 mcu_get_build_time_app(void);
U32 mcu_get_build_time_bb(void);
/*====================================================================================================================*/

#endif

/***********************************************************************************************************************
*                             Copyright 2019 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
